const server = window.server || "";

const getFd = (values) => {
    let fd = "";
    //var data = new FormData();
    for(let p in values) {
        fd += p+"="+encodeURIComponent(values[p])+"&";
        //data.append(p,values[p]);
    }
    if(fd!="") {
        fd = fd.substring(0,fd.length-1);
    }
    return fd;
}

const getFetchParams = (method,params)=>{
    let obj = {
        method:method,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
            'X-Requested-With':'XMLHttpRequest'
        },
    };
    if(server=="") {
        obj.credentials = "include";
    }
    if(params) {
        obj.body = getFd(params);
    }
    return obj;
}

export const doExportCheck = (params) => {
    return new Promise((resolve,reject) => {
        fetch(server+"/api/ec/tool/wf/releasediff/export/check?"+getFd(params),getFetchParams("GET",null)).then(function(response) {
            const datas = response.json();
            if(datas && datas.status && datas.status==="false") {
                reject(datas.error?datas.error:"后端数据处理异常");
            }
            else {
                resolve(datas);
            }
        }).catch(function(ex) {
            reject("后端AJAX异常:",ex);
            console.log('parsing failed', ex)
        });
    });
}

export const doExport = (params) => {
    return new Promise((resolve,reject) => {
        fetch(server+"/api/ec/tool/wf/releasediff/export?"+getFd(params),getFetchParams("GET",null)).then(function(response) {
            const datas = response.json();
            if(datas && datas.status && datas.status==="false") {
                reject(datas.error?datas.error:"后端数据处理异常");
            }
            else {
                resolve(datas);
            }
        }).catch(function(ex) {
            reject("后端AJAX异常:",ex);
            console.log('parsing failed', ex)
        });
    });
}

export const doImportCheck = (params) => {
    return new Promise((resolve,reject) => {
        fetch(server+"/api/ec/tool/wf/releasediff/import/check",getFetchParams("POST",params)).then(function(response) {
            const datas = response.json();
            if(datas && datas.status && datas.status==="false") {
                reject(datas.error?datas.error:"后端数据处理异常");
            }
            else {
                resolve(datas);
            }
        }).catch(function(ex) {
            reject("后端AJAX异常:",ex);
            console.log('parsing failed', ex)
        });
    });
}

export const doImport = (params) => {
    return new Promise((resolve,reject) => {
        fetch(server+"/api/ec/tool/wf/releasediff/import?"+getFd(params),getFetchParams("POST",null)).then(function(response) {
            const datas = response.json();
            if(datas && datas.status && datas.status==="false") {
                reject(datas.error?datas.error:"后端数据处理异常");
            }
            else {
                resolve(datas);
            }
        }).catch(function(ex) {
            reject("后端AJAX异常:",ex);
            console.log('parsing failed', ex)
        });
    });
}